package com.boco.pciv.core.captcha;

//import com.github.bingoohuang.patchca.color.ColorFactory;
//import com.github.bingoohuang.patchca.custom.ConfigurableCaptchaService;
//import com.github.bingoohuang.patchca.filter.predefined.CurvesRippleFilterFactory;
//import com.github.bingoohuang.patchca.utils.encoder.EncoderHelper;
//import com.github.bingoohuang.patchca.word.RandomWordFactory;

import org.patchca.color.SingleColorFactory;
import org.patchca.filter.predefined.CurvesRippleFilterFactory;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.utils.encoder.EncoderHelper;
import org.patchca.word.RandomWordFactory;

import java.awt.*;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

public class MyPatchca {
    //验证码
    private static ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
    private static Random random = new Random();

    static {
//        cs.setColorFactory(new ColorFactory() {
//            @Override
//            public Color getColor(int x) {
//                int[] c = new int[3];
//                int i = random.nextInt(c.length);
//                for (int fi = 0; fi < c.length; fi++) {
//                    if (fi == i) {
//                        c[fi] = random.nextInt(71);
//                    } else {
//                        c[fi] = random.nextInt(256);
//                    }
//                }
//                return new Color(c[0], c[1], c[2]);
//            }
//        });
        cs.setColorFactory(new SingleColorFactory(new Color(25, 60, 170)));
        RandomWordFactory wf = new RandomWordFactory();
        wf.setCharacters("23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ");
        wf.setMaxLength(4);
        wf.setMinLength(4);
        cs.setWordFactory(wf);
    }


    private String code ;
    public void getPatchca(OutputStream os) throws IOException {
        cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));
        //cs.setFilterFactory(new MarbleRippleFilterFactory());
        //cs.setFilterFactory(new DoubleRippleFilterFactory());
        //cs.setFilterFactory(new WobbleRippleFilterFactory());
        //cs.setFilterFactory(new DiffuseRippleFilterFactory());

        code= EncoderHelper.getChallangeAndWriteImage(cs, "png", os);
    }

    public String getCode() {
        return code;
    }
}
